<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_rss_Parser
{
    /**
     * @var f_xml_Xpath
     */
    private $xml;
    private $twitterUser;

    /**
     * @var array
     */
    private $items;

    public function __construct($url, $twitterUser = null, $cacheRefresh)
    {
        $cache = f_core_Cache::getInstance();

        $k = is_null($twitterUser) ? 'rss'.md5($url) : 'tweets'.$twitterUser;

        if($cache->hasCache($k))
        {
            $content = $cache->getCacheContent($k);
        }
        else
        {
            $content = $this->getContent($url);
            $cache->putInCache($k, $content, $cacheRefresh);
        }
        
        $this->twitterUser = $twitterUser;
        $this->xml = f_xml_Xpath::getInstance()->fromString($content);
        $this->items = $this->parseContent();
    }

    /**
     *
     * @param string $url
     * @return f_rss_Parser
     */
    public static function getInstance($url, $twitterUser = null, $cache = 60)
    {
        return new self($url, $twitterUser, $cache);
    }

    public static function getInstanceFromTwitterUser($twitterUser, $cache = 60)
    {
        return self::getInstance('http://twitter.com/statuses/user_timeline/'.$twitterUser.'.rss', $twitterUser, $cache);
    }

    public function getTitle()
    {
        return $this->xml->getElementValueFromXpath('channel/title');
    }

    public function getLink()
    {
        return $this->xml->getElementValueFromXpath('channel/link');
    }

    public function getDescription()
    {
        return $this->xml->getElementValueFromXpath('channel/description');
    }

    public function getPubDate()
    {
        return $this->xml->getElementValueFromXpath('channel/pubDate');
    }

    public function getLang()
    {
        return $this->xml->getElementValueFromXpath('channel/language');
    }

    public function getItems($limit = null)
    {
        if(!is_null($limit))
        {
            return array_slice($this->items, 0, $limit);
        }
        return $this->items;
    }

    private function getContent($url)
    {
        $content = '';

        if ($f = @fopen($url, 'r'))
        {
            while (!feof($f))
            {
                $content .= fgets($f, 4096);
            }
        }

        fclose($f);

        return $content;
    }

    private function parseContent()
    {       
        $result = array();

        $elements = $this->xml->getElementsFromXpath('channel/item');

        if(count($elements) > 0)
        {
            foreach($elements as $element)
            {
                $result[] = f_rss_Item::getInstance($element, $this->twitterUser);
            }
        }

        return $result;
    }

}